package com.cypher.encryptionanddecryption.fragment;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSONObject;
import com.cypher.encryptionanddecryption.R;
import com.cypher.encryptionanddecryption.activity.BluetoothCommunicationActivity;
import com.cypher.encryptionanddecryption.activity.MainActivity;
import com.cypher.encryptionanddecryption.adapter.CommunicationAdapter;
import com.cypher.encryptionanddecryption.bean.CommunicationBean;
import com.cypher.encryptionanddecryption.bluetooth.BluetoothGattManager;
import com.cypher.encryptionanddecryption.bluetooth.BluetoothManagerSingleton;
import com.cypher.encryptionanddecryption.fragment.dialog.BottomDialogFragment;
import com.cypher.encryptionanddecryption.util.ProgressDialogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 维护列表界面
 * @author: 古霸天
 * @date: 2021/3/31
 */
public class MaintainListFragment extends Fragment implements DrawerLayout.DrawerListener{

    private ListView listView;
    private MainActivity mainActivity;
    private BluetoothGattManager gattManager;
    private CommunicationAdapter communicationAdapter;
    private static List<CommunicationBean> communicationBeans;
    private boolean setAdapter = false;
    private BottomDialogFragment fragment;

    static {
        String data = "[{id:1, name:\"OTA升级\"}]";
        communicationBeans = new ArrayList<>();
        CommunicationBean[] beans = JSONObject.parseObject(data, CommunicationBean[].class);
        for (CommunicationBean bean : beans) {
            communicationBeans.add(bean);
        }
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_maintain_list, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
    }

    private void init (View view) {
        // 获取主线程activity
        mainActivity = (MainActivity) BluetoothManagerSingleton.getInstance().getActivity();
        // 初始化列表
        listView = view.findViewById(R.id.maintain_list);
        communicationAdapter = new CommunicationAdapter(view.getContext());

        gattManager = getGattManager();

        // 渲染列表数据
        readerListView();
        setListener();

    }

    @SuppressLint("WrongConstant")
    private void setListener() {
        listView.setOnItemClickListener((AdapterView<?> parent, View v, int position, long id) -> {
            ProgressDialogUtil.showProgressDialog(mainActivity, "加载中...");
            if (fragment == null) {
                fragment = new BottomDialogFragment(position, mainActivity);
            }
            fragment.show(mainActivity.getSupportFragmentManager(), BottomDialogFragment.class.getSimpleName());
        });
    }
    private BluetoothGattManager getGattManager() {
        if (gattManager == null) {
            gattManager = BluetoothManagerSingleton.getInstance().getBluetoothGattManager();
        }
        return gattManager;
    }

    /**
     * 渲染列表数据
     */
    protected void readerListView() {
        communicationAdapter.setCommunicationBeans(communicationBeans);
        communicationAdapter.notifyDataSetChanged();
        // 第一次需要向列表设置适配器
        if (!setAdapter) {
            listView.setAdapter(communicationAdapter);
            setAdapter = true;
        }
    }
    /**
     * 清空列表
     */
    protected void clearList() {
        if (communicationAdapter.getCommunicationBeans() != null) {
            communicationAdapter.getCommunicationBeans().clear();
            communicationAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

    }

    @Override
    public void onDrawerOpened(@NonNull View drawerView) {

    }

    @Override
    public void onDrawerClosed(@NonNull View drawerView) {

    }

    @Override
    public void onDrawerStateChanged(int newState) {

    }

}
